<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">

                <el-form-item label="发货时间" prop="DeliveryTime">
                    <el-date-picker v-model="queryParams.DeliveryTime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>

                <el-form-item label="下单时间" prop="OrderTime">
                    <el-date-picker v-model="queryParams.OrderTime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                
                <el-form-item label="订单ID" prop="order_id">
                    <el-input v-model.trim="queryParams.order_id" placeholder="订单ID" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="物流单号" prop="tracking_number">
                    <el-input v-model.trim="queryParams.tracking_number" placeholder="物流单号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>

                <el-form-item label="包裹状态" prop="payout_status">
                    <el-select v-model="queryParams.payout_status" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in trackList" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>

                <!-- <el-form-item label="查询平台" prop="platform_track">
                    <el-select v-model="queryParams.platform_track" placeholder="请选择查询平台" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in platformOptions" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->

                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-row :gutter="10" class="total-warp">
                <el-col :md="6" :sm="24" v-for="(item, index) in spends" :key="index">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">{{ item.platform }}</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ item.spend }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总共</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends_Total }} </span>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            
            <el-tabs v-model="queryParams.AbnormalState" type="border-card" @tab-click="handleQuery">
                <!-- <el-tab-pane label="全部" :name="undefined"></el-tab-pane> -->
                <el-tab-pane v-for="item in AbnormalStateList" :key="item.value" :label="item.label" :name="item.value"></el-tab-pane>
            </el-tabs>
            <div class="table-wrap">
                <el-table v-loading="loading" :data="tableData" border script>
                    <!-- <el-table-column label="店铺" prop="shop_name" min-width="100"></el-table-column> -->
                    <el-table-column label="订单编号" prop="order_number" min-width="80"></el-table-column>
                    <el-table-column label="物流单号" prop="track_number" min-width="100">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="showDetailDialog(scope.row, 'view')">{{ scope.row.track_number }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="最新物流状态" prop="latest_status" min-width="100">
                        <template slot-scope="scope">
                            {{ scope.row.latest_status | formatLabelByValueFuzzy(trackList) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="上网(天)" prop="SurfInternet" min-width="80"></el-table-column>
                    <el-table-column label="国内运输(天)" prop="Domestic" min-width="100"></el-table-column>
                    <el-table-column label="承运(天)" prop="AcceptCarriage" min-width="80"></el-table-column>
                    <el-table-column label="国外运输(天)" prop="Foreign" min-width="100"></el-table-column>
                    <el-table-column label="签收(天)" prop="SignFor" min-width="80"></el-table-column>
                    <el-table-column label="下单时间" prop="OrderTime" min-width="120"></el-table-column>
                    <el-table-column label="发货时间" prop="DeliveryTime" min-width="120"></el-table-column>
                    <el-table-column label="最后轨迹" prop="time_raw" min-width="200"></el-table-column>
                    <el-table-column label="最后更新时间" prop="latest_time" min-width="120"></el-table-column>
                    <el-table-column label="妥投时间" prop="DeliveredTime" min-width="120"></el-table-column>
                    <el-table-column label="订单号" prop="order_id" min-width="120"></el-table-column>
                    <!-- <el-table-column label="轨迹明细" prop="msg" min-width="160"></el-table-column> -->
                    <!-- <el-table-column label="查询平台" prop="platform_track" min-width="80">
                        <template slot-scope="scope">
                            {{ scope.row.platform_track | formatLabelByValueFuzzy(platformOptions) }}
                        </template>
                    </el-table-column> -->
                    <!-- <el-table-column label="创建时间" prop="create_time" min-width="160"></el-table-column> -->
                    <!-- <el-table-column label="操作" width="100" fixed="right">
                        <template slot-scope="scope">
                            <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">详情</el-button>
                        </template>
                    </el-table-column> -->
                </el-table>
            </div>
        </div>
        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>

        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
    </div>
</template>

<script>
import { STATUS_LIST } from './constant'
import { queryorderValidityList, exportorderValidity, getTrackList } from '@/api/order'
import { getShopList } from '@/api/shop'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
import DetailDialog from './components/DetailDialog.vue'
export default {
    name: 'orderbalanceList',
    components: { DetailDialog },
    data() {
        return {
            STATUS_LIST,
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            salesmanList: [],
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            shopList: [],
            trackList: [],
            AbnormalStateList: [
                {
                    value: 'ALL',
                    label: '无异常'
                },
                {
                    value: '1',
                    label: '超时未上网'
                },
                {
                    value: '2',
                    label: '物流断更'
                },
                {
                    value: '3',
                    label: '包裹退回'
                },
                {
                    value: '4',
                    label: '投递失败'
                },
            ],
            platformOptions: [
                { label: '17track', value: 0, url: 'https://res.17track.net/global-v2/imgs/logo/svg/full_owt_296x48.svg' },
                { label: '718track', value: 1, url: 'https://shopify.fan-zhou.com/storage/system/platform_1.png' },
                { label: '4PXtrack', value: 2, url: 'https://fpx-open-platform-prod.oss-cn-shenzhen.aliyuncs.com/v2/assets/logo.909832ee.svg' },
            ],
            // platformList: [
            //     {
            //         id: 'InStock',
            //         title: '有货'
            //     },
            //     {
            //         id: 'OutOfStock',
            //         title: '缺货'
            //     }
            // ],
            payment_gateway_names: [
                {
                    id: 'NotSentWithin24Hours',
                    title: '24小时未发',
                },
                {
                    id: 'NotSentWithin48Hours',
                    title: '48小时未发',
                },
                {
                    id: 'NotSentFor7Days',
                    title: '7天未发',
                },
                {
                    id: 'More',
                    title: '更多',
                },
            ],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            spends_Total: null,
            spends: [],
            // 查询参数
            queryParams: {
                advertiser_id: undefined,
                type_name: undefined,
                DeliveryTime: [],
                OrderTime: [],
                payment_gateway_names: undefined,
                platform_track: undefined,
                payout_status: 'ALL',
                AbnormalState: 'ALL',
            },
            detailDialog: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const [shopData, TrackData] = await Promise.all([getShopList(), getTrackList()])
            this.shopList = shopData?.data?.list ?? []
            this.trackList = TrackData?.data ?? []
            // this.salesmanList = salesmanData?.data?.list ?? []
            // this.platformList = PlatformData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = row.advertiser_id ? 'edit' : 'add'
        },
        toAdvcampaign(row) {
            this.$router.push({
                path: '/orderbalance/cost/advcampaignCost',
                query: {
                    advertiser_id: row.advertiser_id,
                },
            })
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.DeliveryTime?.length == 2) {
                qyparams.DeliveryTime = [parseTime(qyparams.DeliveryTime[0], 'y-m-d'), parseTime(qyparams.DeliveryTime[1], 'y-m-d')]
            }
            if (qyparams.OrderTime?.length == 2) {
                qyparams.OrderTime = [parseTime(qyparams.OrderTime[0], 'y-m-d'), parseTime(qyparams.OrderTime[1], 'y-m-d')]
            }
            exportorderValidity(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.DeliveryTime?.length == 2) {
                qyparams.DeliveryTime = [parseTime(qyparams.DeliveryTime[0], 'y-m-d'), parseTime(qyparams.DeliveryTime[1], 'y-m-d')]
            }
            if (qyparams.OrderTime?.length == 2) {
                qyparams.OrderTime = [parseTime(qyparams.OrderTime[0], 'y-m-d'), parseTime(qyparams.OrderTime[1], 'y-m-d')]
            }
            queryorderValidityList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                    this.spends_Total = response.data.spends_Total
                    this.spends = response.data?.spends ?? []
                })
                .finally(() => {
                    this.loading = false
                })
        },
        orderbalanceConfirm(row) {
            this.$confirm('是否确认校对', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                orderbalanceConfirm({ id: row.id }).then(() => {
                    this.$message.success('操作成功')
                    this.handleQuery()
                })
            })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style scoped>
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
.total-warp {
    font-size: 18px;
    font-weight: bold;
    padding: 8px 0 16px 0;
}
</style>
